// pages/studentInfo/studentInfo.js
var userModel = require('../../models/user.js')
var request = require('../../operation/operation.js')
var bxt = require('../../utils/bxt.js')

var student;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    student:{},
    canDel:true,
    relations:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    student = getApp().globalData.param
    student.gender = userModel.getStudentGender(student)
    let canDel = false
    if (options.canDel) {
      canDel = true
    } 
    
    this.setData({
      student:student,
      canDel:canDel
    })

    this.getRelation()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 删除学生事件处理函数
   */
  onDelStudent:function() {
    var that = this
   
    wx.showModal({
      title: '删除学生',
      content:'确定删除学生？',
      showCancel: true,
      success: function(res) {
        if (res.confirm) {          
          request.deleteRequest('/students/' + student.sid, {}, true)
            .then(res => {
              var data = res.data;
              if (request.SUCCESSED == data.status) {
                getApp().notificationCenter.post(bxt.DELETE_STUDENT_MESSAGE, { student })

                // 更新用户信息
                getApp().notificationCenter.post(bxt.BIND_ROLE_MESSAGE)

                wx.showToast({
                  title: '删除成功',
                })

                setTimeout(function () {
                  wx.navigateBack({

                  })
                }
                  , 1000)

              }else {
                wx.showToast({
                  title: data.msg,
                  icon: 'none'                 
                })
              }
            })
            .catch(e => {
              console.log(e)
            })
        }
      }
    })
  },

  getRelation:function() {
    var that = this;
    request.getRequest('/relations?studentSid=' + student.sid, {}, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
            that.setData({
              relations:data.items
            })
        }
      })
      .catch(e => {
        console.log(e)
      })
  },

  // removeRole:function(event) {
  //   var that = this
  //   var user = userModel.getCurrentUser()
  //   if (user.sid == event.currentTarget.dataset.relation.student.creatorSid ||
  //     user.sid == event.currentTarget.dataset.relation.user.sid) {
  //       // 学生创建者可以解除任何人的绑定关系、自己只能解除自己的
  //     wx.showActionSheet({
  //       itemList: ['解除绑定'],
  //       success:function(res) {
  //         console.log(res)
  //         if (0 == res.tapIndex) {
  //           wx.showLoading({
  //             title: '请稍候',
  //           })

  //           request.deleteRequest('/relations/' + event.currentTarget.dataset.relation.sid, {}, true)
  //             .then(res => {
  //               wx.hideLoading()
  //               var data = res.data;
  //               if (request.SUCCESSED == data.status) {
  //                 // 更新用户信息
  //                 getApp().notificationCenter.post(bxt.BIND_ROLE_MESSAGE)

  //                 var tmpRelations = that.data.relations
  //                 tmpRelations.pop(event.currentTarget.id)
  //                 that.setData({
  //                   relations: tmpRelations
  //                 })
  //               }else {
  //                 wx.showToast({
  //                   title: data.msg,
  //                   icon: 'none'                  
  //                 })
  //               }
  //             })
  //             .catch(e => {
  //               wx.hideLoading()
  //               console.log(e)
  //             })
  //         }
  //       }
  //     })
  //   }else {
  //     wx.showToast({
  //       title: '您没有权限解除关系绑定',
  //       icon: 'none'
  //     })
  //   }
  // },

  // onNext:function() {
  //   getApp().globalData.param = student
  //   wx.navigateTo({
  //     url: '../editStudent/editStudent?studentSid=' + student.sid,
  //   })
  // }
})